package com.company;

import com.utils.FileUtils;
import com.utils.Map;
import com.utils.MyMapList;

import java.io.IOException;
import java.util.Scanner;

public class SortArray {

    /**
     * 选择排序
     * @throws IOException
     */
    public void selectSort() throws IOException {
        FileUtils fileUtils = new FileUtils();
        fileUtils.readFile("src\\com\\data\\ORDER_SAVE.txt");

        MyMapList maps = fileUtils.readFile("src\\com\\data\\ORDER_SAVE.txt");

        for (int i=0; i<maps.size();i++) {//
            int min_index = i;
            for (int j = i + 1; j < maps.size()-1; j++) {//每轮的比较
                if (maps.get(min_index).getLinkID() > maps.get(j).getLinkID()) {//找到最小值得下标
                    min_index = j;
                }
            }
            if (min_index != i) {//交换最小数[min_index]和第i位数的位置
                Map temp = maps.get(min_index);
                maps.update(maps.get(i),min_index);
                maps.update(temp,i);
            }
        }

        System.out.println("请输入需要进行的选择排序的编号");
        System.out.println("开始编号Start:");
        Scanner scanner = new Scanner(System.in);
        int start = scanner.nextInt();
        System.out.println("结束编号End:");
        int end=scanner.nextInt();

        System.out.println("开始选择排序前：");
        for(int i=start;i<end;i++) {
            System.out.println(maps.get(i).toString());
        }



//        for (int i = 0; i < 30 ;i++)
//        System.out.println(maps.get(i).toString());
//        System.out.println("总共排序数据：" + maps.size() + "条");

    }

    /**
     * 快速排序
     */
    public void quickSort() throws IOException {
        FileUtils fileUtils = new FileUtils();
        fileUtils.readFile("src\\com\\data\\ORDER_SAVE.txt");
        MyMapList maps = fileUtils.readFile("src\\com\\data\\ORDER_SAVE.txt");

        maps.quickSort();
        System.out.println("请输入需要进行的快速排序的编号");
        System.out.println("开始编号Start:");
        Scanner scanner = new Scanner(System.in);
        int start = scanner.nextInt();
        System.out.println("结束编号End:");
        int end=scanner.nextInt();

        System.out.println("开始快速排序前：");
        for(int i=start;i<end;i++) {
            System.out.println(maps.get(i).toString());
        }
//        for (int i = 0; i < 30 ;i++)
//            System.out.println(maps.get(i).toString());
//        System.out.println("总共排序数据：" + maps.size() + "条");

    }


    /**
     * 冒泡排序
     */
    public void bubbleSort_v2() throws IOException {

        FileUtils fileUtils = new FileUtils();
        fileUtils.readFile("src\\com\\data\\ORDER_SAVE.txt");
        MyMapList maps = fileUtils.readFile("src\\com\\data\\ORDER_SAVE.txt");

        maps.bubbleSort();

        System.out.println("请输入需要进行的冒泡排序的编号");
        System.out.println("开始编号Start:");
        Scanner scanner = new Scanner(System.in);
        int start = scanner.nextInt();
        System.out.println("结束编号End:");
        int end=scanner.nextInt();

        System.out.println("开始冒泡排序前：");
        for(int i=start;i<end;i++) {
            System.out.println(maps.get(i).toString());
        }

//        for (int i = 0; i < maps.size() ;i++)
//            System.out.println(maps.get(i).toString());
//        System.out.println("总共排序数据：" + maps.size() + "条");
}


    /**
     * 希尔排序
     */
    public void shellSort() throws IOException {

        FileUtils fileUtils = new FileUtils();
        fileUtils.readFile("src\\com\\data\\ORDER_SAVE.txt");
        MyMapList maps = fileUtils.readFile("src\\com\\data\\ORDER_SAVE.txt");

        int j = 0;
        Map temp ;
        for (int increment = maps.size() / 2; increment > 0; increment /= 2) {
            //System.out.println("increment:" + increment);
            for (int i = increment; i < maps.size(); i++) {

                temp = maps.get(i);
                for (j = i - increment; j >= 0; j -= increment) {

                    if (temp.getLinkID() < maps.get(j).getLinkID()) {
                        maps.update(maps.get(j),j + increment);
                    //    array[j + increment] = array[j];
                    } else {
                        break;
                    }
                }
                maps.update(temp,j+increment);
            //    array[j + increment] = temp;
            }
        }

        System.out.println("请输入需要进行的希尔排序的编号");
        System.out.println("开始编号Start:");
        Scanner scanner = new Scanner(System.in);
        int start = scanner.nextInt();
        System.out.println("结束编号End:");
        int end=scanner.nextInt();

        System.out.println("开始希尔排序前：");
        for(int i=start;i<end;i++) {
            System.out.println(maps.get(i).toString());
        }


//        for (int i = 0; i < maps.size() ;i++)
//            System.out.println(maps.get(i).toString());
//        System.out.println("总共排序数据：" + maps.size() + "条");

    }

    /**
     * 插入排序
     */
    public void insertSort() throws IOException {

        FileUtils fileUtils = new FileUtils();
        fileUtils.readFile("src\\com\\data\\ORDER_SAVE.txt");
        MyMapList maps = fileUtils.readFile("src\\com\\data\\ORDER_SAVE.txt");

        Map value;//待插入元素
        int index;//初始值为待插入元素前一个元素的索引

        for(int i= 1 ; i< maps.size();i++){
            //i从第二个元素开始，默认第一个元素是有序的
            //循环条件是小于数组长度，因为也要将最后一个元素插入到前面的序列
            value = maps.get(i);
            index = i - 1;//初始为前一个元素
            while(index >=0 && value.getLinkID() < maps.get(index).getLinkID()){
                //需要保证index合法
                //每当前面的元素比待插入元素大，就向后移动
                maps.update(maps.get(index),index+1);
                //不用怕覆盖，因为value保存着待插入的值
                index--;
            }
            //当退出循环，表明已经找到了待插入位置，即index + 1
            maps.update(value,index+1);
        }

        System.out.println("请输入需要进行的插入排序的编号");
        System.out.println("开始编号Start:");
        Scanner scanner = new Scanner(System.in);
        int start = scanner.nextInt();
        System.out.println("结束编号End:");
        int end=scanner.nextInt();

        System.out.println("开始插入排序前：");
        for(int i=start;i<end;i++) {
            System.out.println(maps.get(i).toString());
        }


//        for (int i = 0; i < maps.size() ;i++)
//            System.out.println(maps.get(i).toString());
//        System.out.println("总共排序数据：" + maps.size() + "条");

    }

}
